#!/usr/bin/env python
#-*-coding:utf-8-*-

import os
import sys
import shutil
import time



if __name__ == "__main__":

    while True:
        time.sleep(1)
        cur_date = time.strftime("%Y-%m-%d",time.localtime())
        print(cur_date)
        target_path = "/data/wellpilot/chosen_sync_data/" + cur_date
        if not os.path.exists(target_path):
            os.makedirs(target_path, exist_ok=True)

        items = os.listdir("/data/wellpilot/sync_data")
        if len(items) < 5:
            continue
        items = sorted(items)
        items = items[:-2]    
        for item in items:
            if "168" not in item:
                continue

            item_path = os.path.join("/data/wellpilot/sync_data", item)
            if not os.path.isdir(item_path):
                continue

            image_path = os.path.join(item_path, "image")

            if not os.path.exists(os.path.join(image_path, "front_left")):
                continue
            if not os.path.exists(os.path.join(image_path, "front_mid")):
                continue
            if not os.path.exists(os.path.join(image_path, "front_right")):
                continue
            if not os.path.exists(os.path.join(image_path, "rear_left")):
                continue
            if not os.path.exists(os.path.join(image_path, "rear_right")):
                continue
            if not os.path.exists(os.path.join(image_path, "top_left")):
                continue
            if not os.path.exists(os.path.join(image_path, "top_right")):
                continue

            fl_imnames = os.listdir(os.path.join(image_path, "front_left"))
            fm_imnames = os.listdir(os.path.join(image_path, "front_mid"))
            fr_imnames = os.listdir(os.path.join(image_path, "front_right"))
            tl_imnames = os.listdir(os.path.join(image_path, "rear_left"))
            tr_imnames = os.listdir(os.path.join(image_path, "rear_right"))
            rl_imnames = os.listdir(os.path.join(image_path, "top_left"))
            rr_imnames = os.listdir(os.path.join(image_path, "top_right"))

            fl_imnames = set([imname.split('_')[0] for imname in fl_imnames])
            fm_imnames = set([imname.split('_')[0] for imname in fm_imnames])
            fr_imnames = set([imname.split('_')[0] for imname in fr_imnames])
            tl_imnames = set([imname.split('_')[0] for imname in tl_imnames])
            tr_imnames = set([imname.split('_')[0] for imname in tr_imnames])
            rl_imnames = set([imname.split('_')[0] for imname in rl_imnames])
            rr_imnames = set([imname.split('_')[0] for imname in rr_imnames])

            intsc = fl_imnames.intersection(fr_imnames, fm_imnames, tl_imnames, tr_imnames, rl_imnames, rr_imnames)
            uni = fl_imnames.union(fr_imnames, fm_imnames, tl_imnames, tr_imnames, rl_imnames, rr_imnames)
            if len(uni)==0:
                shutil.rmtree(item_path)
                continue
            percent = float(len(intsc))/len(uni)
            print("%d files in dir" % len(intsc))
            print("%f valid image." % percent)
            if (len(intsc) > 80 and percent > 0.7):
                shutil.move(item_path, os.path.join(target_path, item))
                print("move", item_path)
            else:
                shutil.rmtree(item_path)
                print("remove", item_path)
            print("--------------------------")
        time.sleep(100)
